<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_widget extends BZ_Model
{
/**
 * @section BISKITZ_FRONT Widget Model
 * @description the model for biskitz front-end content
 */

	/**
	 * @function content_generate
	 * @description generate the content based on the category specified
	 */
	public function content_generate($prm_dbase = 'db_default', $prm_table_where = '', $prm_fields_order = '', $prm_record_limit = 5, $prm_paging_use = 0)
	{
		$fields = 'id, type, title, slug'
			. ', date, date_format(date, "%Y") as year, date_format(date, "%m") as month, date_format(date, "%d") as day'
			. ', content, file_name, url';

		$table = array
			(
				'NAME' => 'biskitz_front_content'
				, 'FIELDS' => $fields . ', comments'
			);


		$content_data =	$this->M_database->datas
			(
				$prm_dbase
				, $table['NAME']
				, array_merge
					(
						array
							(
								'language__id' => $this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id']
							)
						, $prm_table_where
					)
				, $table['FIELDS']
				, FALSE
				, $prm_fields_order
				, ''
				, $prm_record_limit, $prm_paging_use
				, FALSE
				, FALSE
			);
		if (is_array($content_data))
		{
			foreach ($content_data as $rows)
			{
				/**
				 * @description generate the content detail
				 */
				$return_value[] = array
					(
						/**
						 * @description content detail
						 */
						'id' => $rows['id']
						, 'link' => $rows['type'] == 'Post' ?
								site_url(array('read', 'posts', $rows['year'], $rows['month'], $rows['day'], $rows['slug']))
								: site_url(array('read', 'pages', $rows['slug']))
						, 'title' => $rows['title']
						, 'date' => $rows['day']
						, 'month' => $rows['month']
						, 'year' => $rows['year']
						, 'content' => $rows['content']
						, 'file_name' => $rows['file_name']
						, 'url' => $rows['url']
					);
			}
		}
		else
		{
			$return_value = array();
		}


		return $return_value ;
	}


	/**
	 * @function category_generate
	 * @description generate the category list recursively
	 */
	public function category_generate
		(
			$prm_dbase = 'db_default'
			, $prm_url = array()
			, $prm_language_id = '1'
			, $prm_type = 'Post' // available: Post, Links
			, $prm_parent_as_link = TRUE
			, $prm_sub_use = TRUE
			, $prm_content_show = FALSE
			, $prm_counter_use = TRUE
			, $prm_order = 'category_name'
			, $prm_parent_id = 0
			, $prm_parent_tag_open = '<b class="category_parent">', $prm_parent_tag_close = '</b>'
			, $prm_counter_tag_open = '<sup class="category_counter">', $prm_counter_tag_close = '</sup>'
		)
	{
		$category_data =	$this->M_database->datas
			(
				$prm_dbase
				, 'biskitz_front_category a'
				, array
					(
						'type' => $prm_type
						, 'parent_id' => $prm_parent_id
					)
				, 'id'
					. ', (select name from bz_biskitz_front_category_lang c where a.id = c.front_category__id and c.language__id = "'.$prm_language_id.'") as category_name'
					. ', (select slug from bz_biskitz_front_category_lang c where a.id = c.front_category__id and c.language__id = "'.$prm_language_id.'") as category_slug'
				, FALSE
				, $prm_order
				, ''
				, '', ''
				, FALSE
				, FALSE
			);


		if (is_array($category_data))
		{
			foreach ($category_data as $rows)
			{
				/**
				 * @description is parent as a link?
				 */
				if ($prm_parent_as_link == TRUE)
				{
					$category_anchor = anchor
						(
							site_url(array_merge($prm_url, array($rows['category_slug'])))
							, $rows['category_name']
							, array('title' => $rows['category_name'])
						);
				}
				else
				{
					$category_anchor = $prm_parent_tag_open . $rows['category_name'] . $prm_parent_tag_close;
				}

				/**
				 * @description need the counter of the content?
				 */
				$counter_count = '';
				if ($prm_counter_use == TRUE)
				{
					$counter_count = $this->M_database->total_rows
						(
							$prm_dbase
							, 'biskitz_front_content'
							, array
								(
									'type' => $prm_type
									, 'front_category__id' => $rows['id']
									, 'language__id' => $prm_language_id
									, 'status' => 'Publish'
								)
						);

					if ($counter_count == 0)
					{
						$counter_count = '';
					}
					else
					{
						$counter_count = $prm_counter_tag_open . $counter_count . $prm_counter_tag_close;
					}
				}


				$return_value[$category_anchor . $counter_count] = $category_anchor . $counter_count;


				/**
				 * @description need to show the content detail?
				 */
				if ($prm_content_show == TRUE)
				{
					$return_value[$category_anchor . $counter_count] = $this->category_list
						(
							$prm_dbase
							, $prm_language_id, $prm_type
							, $rows['id']
						);
				}

				/**
				 * @description need to show the sub category?
				 */
				if ($prm_sub_use == TRUE)
				{
					$category_sub_count = $this->M_database->total_rows
						(
							$prm_dbase
							, 'biskitz_front_category'
							, array
								(
									'type' => $prm_type
									, 'parent_id' => $rows['id']
								)
						);

					if ($category_sub_count > 0)
					{
						$category_generate = $this->category_generate
							(
								$prm_dbase
								, $prm_url
								, $prm_language_id
								, $prm_type
								, $prm_parent_as_link
								, $prm_sub_use
								, $prm_content_show
								, $prm_counter_use
								, $prm_order
								, $rows['id']
								, $prm_parent_tag_open, $prm_parent_tag_close
								, $prm_counter_tag_open, $prm_counter_tag_close
							);

						$return_value[$category_anchor . $counter_count] = ($prm_content_show == TRUE)
							? array_merge
								(
									$return_value[$category_anchor . $counter_count]
									, $category_generate
								)
							: $category_generate;
					}
				}
			}


			return $return_value;
		}
	}


	/**
	 * @function category_list
	 * @description generate the category type
	 */
	public function category_list
		(
			$prm_dbase = 'db_default'
			, $prm_language_id = '1'
			, $prm_type = 'Post' // available: Post, Links
			, $prm_category_id = 1
		)
	{
		$use_db = $this->{$prm_dbase};

		$use_db
			->select
				(
					'id, type, title, slug'
					. ', date_format(date, "%Y") as year, date_format(date, "%m") as month, date_format(date, "%d") as day'
					. ', url'

					, FALSE
				)
			->from('biskitz_front_content')
			->where
				(
					array
						(
							'language__id' => $prm_language_id
							, 'front_category__id' => $prm_category_id
							, 'type' => $prm_type
							, 'status' => 'Publish'
						)
				)
			->order_by('orders');
		$query = $use_db->get();

		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $rows)
			{
				switch ($prm_type)
				{
					case 'Post':
						$prm_url = site_url(array('read', 'posts', $rows['year'], $rows['month'], $rows['day'], $rows['slug']));
						break;

					case 'Links':
						$prm_url = $rows['url'];
						break;
				}


				$return_value[$rows['title']] = anchor
					(
						$prm_url
						, htmlentities($rows['title'])
						, array
							(
								'target' => $rows['type'] == 'Links' ? '_blank' : ''
								, 'title' => $rows['title']
							)
					);
			}

		}
		else
		{
			$return_value = array();
		}


		return $return_value;
	}


	/**
	 * @function social_network_generate
	 * @description generate the social_network
	 */
	public function social_network_generate($prm_dbase = 'db_default')
	{
		$use_db = $this->{$prm_dbase};

		$use_db
			->select('type, template, b.id as user_id, name, description, tooltips')
			->from('bz_biskitz_front_social_network a, bz_biskitz_front_social_network_user b')
			->where('a.id = b.front_social_network__id and visibility = "Show"')
			->order_by('type, user_id');
		$query = $use_db->get();

		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $rows)
			{
				$marker_sign = array('{user_id}', '{name}', '{description}', '{tooltips}');
				$marker_replace = array($rows['user_id'], $rows['name'], $rows['description'], $rows['tooltips']);

				$return_value[] = array(str_replace($marker_sign, $marker_replace, $rows['template']));
			}


			return $return_value;
		}
	}


	/**
	 * @function language_list
	 * @description generate the available language
	 */
	public function language_list
		(
			$prm_dbase = 'db_default'
			, $prm_type = 'image' //image, combo
			, $prm_lang_selection = 'biskitz_front_widget_language_selection'
		)
	{
		$return_value = '';
		$content_data =	$this->M_database->datas
			(
				$prm_dbase
				, 'biskitz_language'
				, array()
				, 'abbreviation as combo_key, name as combo_value'
				, FALSE
				, 'name'
				, ''
				, ''
				, '', ''
				, FALSE
				, FALSE, ''
				, ($prm_type == 'image') ? FALSE : TRUE
			);

		if (is_array($content_data))
		{
			switch ($prm_type)
			{
				case 'image':
					/**
					 * @description init the variables
					 */
					$variables = array
						(
							'IMAGE' => array
								(
									'PATH' => array
										(
											'themes'
											, $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_THEME']
											, 'images'
											, 'language'
										)
								)
						);

					foreach ($content_data as $rows)
					{
						$image_attr = array
							(
								'src' => site_url(array_merge($variables['IMAGE']['PATH'], array($rows['combo_key'].'.png')))
								, 'border' => 0
								, 'alt' => $rows['combo_value']
							);

						$return_value .= '<a href="#" onClick="javascript: language_change(\''.$rows['combo_key'].'\')" title="'.$rows['combo_value'].'" id="txt_language">'.img($image_attr, TRUE).'</a>&nbsp;';
					}
					break;


				case 'combo':
					$return_value = form_dropdown
						(
							'txt_language'
							, array_merge
								(
									array('-' => $this->lang->line($prm_lang_selection))
									, $content_data
								)
							, array()
							, 'id="txt_language"'
						);
					break;
			}
		}


		return $return_value;
	}


	/**
	 * @function language_change
	 * @description change the language settings
	 */
	public function language_change($prm_language_abbr = 'en')
	{
		@session_start();
		$session_language_data = array('session_biskitz_front_language_abbr' => $prm_language_abbr);
		$this->session->set_userdata($session_language_data);
	}



}

/** End of file m_widget.php */
/** Location: ./biskitz_mpc/biskitz/models/biskitz/m_widget.php */